package com.inspire.tank;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

/**
 * @author inspire
 * @description
 * @date 2022/4/24 14:19
 */
public class TankFrame extends Frame{
    GameModel gm=GameModel.getInstance();
    public static final int GAME_WIDTH=1920,GAME_HEIGHT=1080;

    public TankFrame(){
        setSize(GAME_WIDTH,GAME_HEIGHT);
        setResizable(false);
        setTitle("Tank war");
        setVisible(true);

        this.addKeyListener(new MyKeyListener());

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    Image offScreenImage=null;
    @Override
    public void update(Graphics g) {
        if(offScreenImage==null){
            offScreenImage=this.createImage(GAME_WIDTH,GAME_HEIGHT);
        }

        Graphics gOffScreen=offScreenImage.getGraphics();
        Color c=gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);
        gOffScreen.setColor(c);

        paint(gOffScreen);
        g.drawImage(offScreenImage,0,0,null);
    }

    @Override
    public void paint(Graphics g) {
        gm.paint(g);
    }

    class MyKeyListener extends KeyAdapter{
        boolean bL=false;
        boolean bR=false;
        boolean bU=false;
        boolean bD=false;

        @Override
        public void keyPressed(KeyEvent e) {//按键时被调用
            int key=e.getKeyCode();
            switch (key){
                case KeyEvent.VK_LEFT:
                    bL=true;
                    break;
                case KeyEvent.VK_UP:
                    bU=true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR=true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD=true;
                    break;
                case KeyEvent.VK_S:
                    gm.save();
                case KeyEvent.VK_L:
                    gm.load();
                default:
                    break;
            }

            setMainTankDir();
        }

        private void setMainTankDir() {
            Tank myTank=gm.getMainTan();
            if(!bL&&!bU&&!bR&&!bD){
                myTank.setMoving(false);
            }else{
                myTank.setMoving(true);
            }

            if(bL){
                myTank.setDir(Dir.LEFT);
            }
            if(bR){
                myTank.setDir(Dir.RIGHT);
            }
            if(bU){
                myTank.setDir(Dir.UP);
            }
            if(bD){
                myTank.setDir(Dir.DOWN);
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {//按键释放时被调用
            int key=e.getKeyCode();
            switch (key){
                case KeyEvent.VK_LEFT:
                    bL=false;
                    break;
                case KeyEvent.VK_UP:
                    bU=false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR=false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD=false;
                    break;
                case KeyEvent.VK_CONTROL:
                    gm.getMainTan().fire();
                default:
                    break;
            }
            setMainTankDir();
        }
    }
}
